package com.haier.npt.logs;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import ch.qos.logback.classic.pattern.MethodOfCallerConverter;
import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.LayoutBase;

import com.alibaba.fastjson.JSON;

public class NptLayout extends LayoutBase<ILoggingEvent> {
	String serverName;

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	static MethodOfCallerConverter methodConverter = new MethodOfCallerConverter();
	static ThrowableProxyConverter throwableConverter = new ThrowableProxyConverter();

	public String doLayout(ILoggingEvent event) {

		HashMap<String, Object> map = new HashMap<>();
		map.put("createTime", formatter.format(new Date(event.getTimeStamp())));
		map.put("level", event.getLevel().toString());
		map.put("className", event.getLoggerName());
		map.put("threadName", event.getThreadName());
		map.put("content", event.getFormattedMessage());
		map.put("serverName", serverName);
		map.put("userName", LogThreadLocal.userName.get());
		map.put("projectId", LogThreadLocal.projectId.get());
		map.put("enterpriseId", LogThreadLocal.enterpriseId.get());

		map.put("method", methodConverter.convert(event));

		map.put("i18n", throwableConverter.convert(event));

		return JSON.toJSONString(map) + "\n";
	}

}